<?php

/**
 * Description of ContentUserHistory
 *
 * @author Honza, Kevin 
 */
class ContentUserHistory implements IMainBody {

  public function getContent() {
    $content = '';
    $content .= '<h2>History of borrowings</h2>';

    $auth = Auth::getInstance();
    $identity = $auth->getIdentity();
    $userID = $identity['id'];

    $cols = 'id_borrowing,title, DATE_FORMAT(date_of_returning, \'%Y-%m-%d\') as returned_at';
    $sql = 'select ' . $cols . ' from ' . TBL_BORROWINGS . '';
    $sql .= ' join ' . TBL_BOOK_PRINTS . ' using (id_book_print)';
    $sql .= ' join ' . TBL_BOOKS . ' using (id_book)';
    $sql .= ' where id_user = ' . $userID . ' and date_of_returning is not null';
    $sql .= ' order by title';


    $result = DB::sql($sql);

    if (empty($result)) {
      return '<p>No historic borrowings</p>';
    }

    $table = '<table>';
    $table .= '<tr>';
    $table .= '<th>Title</th><th>Returned at</th>';
    $table .= '</tr>';
    foreach ($result as $row) {
      $table .= '<tr>';
      $table .= '<td>' . $row['title'] . '</td>';
      $table .= '<td>' . $row['returned_at'] . '</td>';
      $table .= '</tr>';
    }
    $table .= '';
    $table .= '</table>';
    $content .= $table;

    return $content;
  }

}

?>
